package com.yizhi.controller;


import cn.hutool.core.bean.BeanUtil;
import com.yizhi.domain.dto.WorksManageDTO;
import com.yizhi.domain.entityDO.WorksManageDO;
import com.yizhi.domain.vo.WorksManageVO;
import com.yizhi.service.IWorksManageService;
import com.yizhi.util.ApiResult;
import com.yizhi.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考试管理表 前端控制器
 * </p>
 *
 * @author zhouyihe
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/works-manage")
@Api(tags = "作品管理控制器")
@Slf4j
public class WorksManageController {
    @Autowired
    private IWorksManageService worksManageService;
    
    /**
     * 新增作品
     * @param worksManageDO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增作品")
    public ApiResult add(@RequestBody WorksManageDO worksManageDO){
        log.info("新增作品:{}",worksManageDO);
        boolean save = worksManageService.save(worksManageDO);
        return save ? ApiResult.success("增加成功"):ApiResult.error("增加失败");
    }
    
    /**
     * 分页查询作品
     * @param worksManageDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询作品")
    public ApiResult<PageResult> queryWorksPage(@RequestBody WorksManageDTO worksManageDTO){
        log.info("分页查询作品:{}",worksManageDTO);
        return ApiResult.success(worksManageService.pageQuery(worksManageDTO));
    }
    
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("批量删除")
    public ApiResult delete(@PathVariable List<Integer> ids){
        log.info("批量删除id为:{}的作品",ids);
        boolean b = worksManageService.removeBatchByIds(ids);
        return b ? ApiResult.success("删除成功"):ApiResult.error("删除失败");
    }
    
    /**
     * 更新作品
     * @param worksManageDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新作品")
    public ApiResult update(@RequestBody WorksManageDTO worksManageDTO){
        log.info("更新作品:{}",worksManageDTO);
        WorksManageDO worksManageDO = BeanUtil.copyProperties(worksManageDTO, WorksManageDO.class);
        boolean b = worksManageService.updateById(worksManageDO);
        return b ? ApiResult.success("更新成功"):ApiResult.error("更新失败");
    }
}
